/*************************************************************************
	> File Name: client_recv.c
	> Author: Maureen 
	> Mail: 1437867946@qq.com 
	> Created Time: Fri 21 May 2021 05:08:06 PM CST
 ************************************************************************/

#include "head.h"

extern int sockfd;

void *client_recv(void *arg) {
    while (1) {
        struct FootBallMsg msg;
        struct User user;
        bzero(&msg, sizeof(msg));

        recv(sockfd, (void *)&msg, sizeof(msg), 0);

        strcpy(user.name, msg.name);
        user.team = msg.team;
        
        DBG("msg.type = %d\n", msg.type);

        if (msg.type & FT_HEART) { //收到心跳就回一个心跳包
            DBG(RED"[Server] : HeartBeat ❤\n"NONE);
            msg.type = FT_ACK;
            send(sockfd, (void *)&msg, sizeof(msg), 0);
            DBG(YELLOW"[Client] : Response ACK to Server\n\n"NONE);
        } else if (msg.type & FT_MSG) {
            DBG(GREEN"[Server] : "NONE"%s\n", msg.msg);
        } else if (msg.type & FT_WALL) {
            DBG(GREEN"[Server] : WALL message\n\n"NONE);
        } else if (msg.type & FT_FIN) {
            DBG(GREEN"[Server]: Server is going to stop.\n"NONE);
            close(sockfd);
            exit(0);
        } else if (msg.type & FT_MAP) {
            DBG(GREEN"[Server] : Game Map message\n\n"NONE);
        } else if(msg.type & FT_ACK) {
            DBG(GREEN"[Server] : ACK Message\n\n"NONE);
        } else {
            DBG(GREEN"Msg unsupport\n"NONE);
        }
    }
}
